package cn.xylin.zjwh.crack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author XyLin
 * @date 2020年2月17日 12:51:54
 * MyInternet.java 用于访问网络资源
 **/
public class Http {
    private static final String REQUEST_POST = "POST";
    private static final int REQUEST_TIME_OUT = 30000;
    public static final String PROJECT_URL = "https://gitee.com/linxylin/zjwhCrack";
    public static final String UPDATE_URL = PROJECT_URL + "/raw/master/update.json";
    public static final String ANTI_CODE_URL = PROJECT_URL + "/raw/master/antiCode.json";
    public static final String DOWNLOAD_URL = "https://www.lanzous.com/ajaxm.php";
    public static String ajaxmReferershUrl = "";
    public static final String DOWNLOAD_URL_FORMAT = "action=downprocess&sign=%s&ves=1";

    /**
     * @param requestUrl 需要访问的URL网址（字符串）
     * @param params     访问网址如需要POST方式且带入参数，即需要传入该参数。参数写法示例"name=XyLin"
     * @return 返回访问网址后获得的返回结果
     * @throws IOException 抛出io异常
     */
    private static HttpURLConnection generateRequest(String requestUrl, String... params) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(requestUrl).openConnection();
        connection.setConnectTimeout(REQUEST_TIME_OUT);
        connection.setReadTimeout(REQUEST_TIME_OUT);
        connection.setRequestProperty("User-Agent", "");
        if (params.length > 0) {
            if (DOWNLOAD_URL.equals(requestUrl)) {
                connection.setRequestProperty("referer", ajaxmReferershUrl);
            }
            connection.setRequestMethod(REQUEST_POST);
            StringBuilder builder = new StringBuilder();
            OutputStream stream = connection.getOutputStream();
            for (String param : params) {
                builder.append(param);
                if (!(params[(params.length - 1)].equals(param))) {
                    builder.append("&");
                }
            }
            stream.write(builder.toString().trim().getBytes(StandardCharsets.UTF_8));
            stream.flush();
            stream.close();
        }
        connection.connect();
        return connection;
    }

    /**
     * @param requestUrl 需要访问并获取结果的网址
     * @param params     POST方法附带参数
     * @return 返回访问网址后解析获得的返回字符串
     * @throws IOException 抛出IO异常
     */
    public static String readUrlString(String requestUrl, String... params) throws IOException {
        HttpURLConnection connection = generateRequest(requestUrl, params);
        InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String readLine;
        StringBuilder builder = new StringBuilder();
        while ((readLine = reader.readLine()) != null) {
            builder.append(readLine);
        }
        reader.close();
        inputStreamReader.close();
        connection.disconnect();
        return builder.toString().trim();
    }
}
